#!/usr/bin/python
# -*- coding=utf-8 -*-
# author : fzk
# date: 20220218
# version: 0.1
# 导入解析包
from bs4 import BeautifulSoup
from pandas.core.frame import DataFrame

# sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>", 'lxml')
# a = sibling_soup.find_all("a")[0]("b")[0].text
# b = sibling_soup.c.previous_sibling
# c = sibling_soup.a.b.text
# print(a)
# print(c)


def StrToXml(elm, keys, far=''):
    ke, ouval,pat,oup = [],[],[],[]
    tree = BeautifulSoup(elm, 'lxml')
    if far:
        trr = tree(far)
        # print(trr)
        for i in keys:
            val, path = [], []
            for u in trr:
                v = u(i)
                for e in v:
                    val.append(e.text)
                    path.append(e)
            ke.append(val)
            pat.append(path)

    else:
        for i in keys:
            val,path = [],[]
            v = tree(i)
            for e in v:
                # e.string="1234"
                val.append(e.text)
                path.append(e)
                # print(type(e))
            ke.append(val)
            pat.append(path)
    date_k = DataFrame(ke)
    date_p = DataFrame(pat)
    # print(type(date_p[0].values))
    clok = date_k.columns
    clop = date_p.columns
    # print(clop)
    for t in clok:
        k = date_k[t].values.tolist()
        ouval.append(k)
    for t in clop:
        p = date_p[t].values.tolist()
        oup.append(p)
    # print(tree.prettify())
    return ouval,oup,keys,tree


if __name__ == '__main__':
    # html_doc = "<test><country name=\"Liechtenstein\"><rank updated=\"yes\">2</rank><year>2008</year><yea>2008</yea><gdppc>141100</gdppc><neighbor name=\"Austria\" direction=\"E\" /><neighbor name=\"Switzerland\" direction=\"W\" /><ran updated=\"ys\">2</ran></country><country name=\"Liechtenstein\"><rank updated=\"yes\">2</rank><year>2011</year><gdppc>141102</gdppc><neighbor name=\"Austria\" direction=\"E\" /><neighbor name=\"Switzerland\" direction=\"W\" /><ran updated=\"ys\">2</ran></country><country name=\"Liechtenstein\"><rank updated=\"yes\">2</rank><year>2009</year><gdppc>141105</gdppc><neighbor name=\"Austria\" direction=\"E\" /><neighbor name=\"Switzerland\" direction=\"W\" /><ran updated=\"ys\">2</ran></country><country name=\"Liechtenstein\"><rank updated=\"yes\">2</rank><year>2015</year><gdppc>121106</gdppc><neighbor name=\"Austria\" direction=\"E\" /><neighbor name=\"Switzerland\" direction=\"W\" /><ran updated=\"ys\">2</ran></country></test>"
    html_doc = '''<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E" />
        <neighbor name="Switzerland" direction="W" />
    <ran updated="ys">2</ran></country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N" />
    <ran updated="ys">2</ran></country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W" />
        <neighbor name="Colombia" direction="E" />
    <ran updated="ys">2</ran></country>
</data>'''
    out = StrToXml(html_doc, ['year', 'gdppc', 'yeat'],'country')
    # print(out)
    print(out[0])
    out[1][0][0].string = "test0227"
    print(type(out[3]))
    # print(str(out[3]))
    # print(out[2].tostring)